package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.dao.BizFlowAppDao;
import com.central.app.model.BizFlowApp;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class PmWorkorderHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
//    @Autowired
//    private BizFlowAppDao flowAppDao;


    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        //保存时 将维修工段的值赋予deptcode
//        String wonum = MapUtil.getStr(data, "WONUM");
//        String maintenancesection =MapUtils.getString(data,"maintenancesection");
//        handlerService.updateBySql("UPDATE bl_workorder set deptcode='"+maintenancesection+"' where wonum='"+wonum+"'");
        String wonum = MapUtil.getStr(data, "wonum");
        if("gq_workorder".equals(objName)){
            String jpnum = MapUtil.getStr(data, "jpnum");

            if(jpnum!=null){
                List<Map<String, Object>> datas = new ArrayList<>();
                // 1.查询作业任务信息
                List<Map<String, Object>> list = handlerService.findBySql("select jobtask, abstract from gq_jobtask t where t.jpnum = '" + jpnum + "'");
                if (CollectionUtils.isNotEmpty(list)) {
                    // 组织插入数据
                    for (Map<String, Object> map : list) {
                        Map<String, Object> insertMap = new HashMap<>();
                        insertMap.put("tasknum", map.get("jobtask"));// 任务编码
                        insertMap.put("taskdesc", map.get("abstract"));// 任务描述
                        insertMap.put("wonum", wonum);// 工单编码
                        datas.add(insertMap);
                    }
                    // 插入计划作业任务信息
                    handlerService.batchInsert("wptaskApp", "gq_wptask", datas);
                    //插入实际工作子表
                    handlerService.batchInsert("wptaskApp", "gq_acttask", datas);
                }

                // 2.查询作业计划-人员信息
                list = handlerService.findBySql("select labor, quantity, craft from gq_joblabor t where t.jpnum = '" + jpnum + "'");
                // 组织插入数据
                if (CollectionUtils.isNotEmpty(list)) {
                    datas = new ArrayList<>();
                    for (Map<String, Object> map : list) {
                        Map<String, Object> insertMap = new HashMap<>();
                        insertMap.put("labor", map.get("labor"));// 员工ID
                        insertMap.put("quantity", map.get("quantity"));// 数量
                        insertMap.put("craft", map.get("craft"));// 工种
                        insertMap.put("wonum", wonum);// 工单编码
                        datas.add(insertMap);
                    }
                    // 插入作业人员信息
                    handlerService.batchInsert("wplaborApp", "gq_wplabor", datas);
                }
                // 3.查询作业计划-物料信息
                list = handlerService.findBySql("select itemnum, itemqty, unitcost, linecost from gq_jobitem t where t.jpnum = '" + jpnum + "'");
                // 组织插入数据
                if (CollectionUtils.isNotEmpty(list)) {
                    datas = new ArrayList<>();
                    for (Map<String, Object> map : list) {
                        Map<String, Object> insertMap = new HashMap<>();
                        insertMap.put("itemnum", map.get("itemnum"));// 物料编码
                        insertMap.put("itemdesc", "");// 物料名称
                        insertMap.put("itemqty", map.get("itemqty"));// 数量
                        insertMap.put("unitcost", map.get("unitcost"));// 单位成本
                        insertMap.put("linecost", map.get("linecost"));// 行成本
                        insertMap.put("wonum", wonum);// 工单编码
                        datas.add(insertMap);
                    }

                    // 插入作业人员信息
                    handlerService.batchInsert("wpmaterialApp", "gq_wpmaterial", datas);
                }
            }


        }
        //作业计划插入数据之后，实际情况也需要插入，修改或者删除的时候
        if("gq_wptask".equals(objName)){
            //删除实际情况，查询作业计划，重新插入
            wonum = MapUtil.getStr(data, "wonum");
            String sql = "delete from gq_acttask where wonum = '"+wonum+"'";
            handlerService.updateBySql(sql);
            String sql1 = "select * from gq_wptask where wonum = '"+wonum+"'";
            List<Map<String, Object>> list = handlerService.findBySql(sql1);

            handlerService.batchInsert("wpmaterialApp", "gq_acttask", list);
        }

        super.afterSave(app, objName, data, isUpdate);
    }

    //保存前调用
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {



        //如果是修改，如果作业计划编号不为空，先删除原来的作业计划编号插入的人员，物料，作业任务数据。保存后代码那边添加最新的
        if("gq_workorder".equals(objName)&&isUpdate){
            //String jpnum = MapUtil.getStr(data, "jpnum");
            String wonum = MapUtil.getStr(data, "wonum");

            //查询原作业计划编号
            String sql = "select jpnum from gq_workorder where wonum =  '" + wonum + "'";
            List<Map<String, Object>> workList = handlerService.findBySql(sql);

            if(CollectionUtils.isNotEmpty(workList)&&workList.get(0).get("jpnum")!=null){
                String sql2 = "delete from gq_wptask where wonum =  '" + wonum + "'";//计划作业任务
                handlerService.updateBySql(sql2);
                String sql3 = "delete from gq_wplabor where wonum =  '" + wonum + "'";//人员信息
                handlerService.updateBySql(sql3);
                String sql4 = "delete from gq_wpmaterial where wonum =  '" + wonum + "'";//物料信息
                handlerService.updateBySql(sql4);
                String sql5 = "delete from gq_acttask where wonum =  '" + wonum + "'";//实际工作任务
                handlerService.updateBySql(sql5);
            }


        }




    }



}
